package com.scsj.supermarket.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.AdressBean;
import com.scsj.supermarket.bean.DeliveryAddressBean;
import com.scsj.supermarket.utils.CheckMobileUtils;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.utils.ToastMiddleUtils;

import java.io.Serializable;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 地址列表
 */
public class AdressAdapter extends RecyclerView.Adapter<AdressAdapter.MyViewHolder> {

    private Context mContext;
    private List<DeliveryAddressBean.DataBean> mList;
    private LayoutInflater mInflater;

    public AdressAdapter(Context context, List<DeliveryAddressBean.DataBean> data) {
        this.mContext = context;
        this.mList = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_adress, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.adress_name_text.setText(mList.get(position).getUserDeliveryAddress().getReceiverName());
        holder.adress_phone_text.setText(mList.get(position).getUserDeliveryAddress().getReceiverPhone());
        holder.adress_address_text.setText(mList.get(position).getLocation().getAddress());
        if (mList.get(position).getUserDeliveryAddress().getTag() != null && //标签
                mList.get(position).getUserDeliveryAddress().getTag().length() > 0) {

            if (holder.adress_state_gray_tv.getVisibility() == View.INVISIBLE) {
                holder.adress_state_gray_tv.setVisibility(View.VISIBLE);
            }
            holder.adress_state_gray_tv.setText(mList.get(position).getUserDeliveryAddress().getTag());

        } else {
            if (holder.adress_state_gray_tv.getVisibility() == View.VISIBLE) {
                holder.adress_state_gray_tv.setVisibility(View.INVISIBLE);
            }

        }
        if (mList.get(position).getUserDeliveryAddress().getIsDefult() == 1) {//默认地址
            holder.address_img.setImageResource(R.mipmap.cart_selected);
        } else {
            holder.address_img.setImageResource(R.mipmap.cart_unselected);
        }

        if (mList.get(position).getUserDeliveryAddress().getIsDefult() == 1) {//默认地址
            holder.adress_default_tv.setVisibility(View.VISIBLE);
        } else {
            holder.adress_default_tv.setVisibility(View.GONE);
        }

        holder.right_editor_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SkipUtils.toAdressEditWithData(mContext,mList.get(position));
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout adress_ll_item;
        public TextView adress_name_text;
        public TextView adress_phone_text;
        public TextView adress_address_text;
        public TextView tv_item_default;
        public TextView tv_item_delete;
        public TextView adress_state_gray_tv;
        public ImageView address_img;
        public TextView adress_default_tv;
        public RelativeLayout right_editor_rl;

        public MyViewHolder(View itemView) {
            super(itemView);
            adress_ll_item = (LinearLayout) itemView.findViewById(R.id.adress_ll_item);
            adress_name_text = (TextView) itemView.findViewById(R.id.adress_name_text);
            adress_phone_text = (TextView) itemView.findViewById(R.id.adress_phone_text);
            adress_address_text = (TextView) itemView.findViewById(R.id.adress_address_text);
            tv_item_default = itemView.findViewById(R.id.tv_item_default);
            tv_item_delete = itemView.findViewById(R.id.tv_item_delete);
            adress_state_gray_tv = itemView.findViewById(R.id.adress_state_gray_tv);
            address_img = itemView.findViewById(R.id.address_img);
            adress_default_tv = itemView.findViewById(R.id.adress_default_tv);
            right_editor_rl = itemView.findViewById(R.id.right_editor_rl);
//            tv_item_default.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(mContext, "设为默认", Toast.LENGTH_SHORT).show();
//                }
//            });
        }
    }

}
